﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;

namespace OK.SCOM.Authoring.Tools.MPElementsExtract
{
	internal class MPElementTypeReportResource: MPElementTypeBase
	{
		private string m_name;
		private string m_data;
		private XmlNode m_datanode;

		internal override string Name
		{
			get
			{
				return "Report Resource (placemarks are not used - content will be replaced on embedding)";
			}
		}

		internal override bool SupportsEmbed
		{
			get { return true; }
		}

		internal override bool SupportsExtractReplaceWithPlaceMarks
		{
			get { return true; }
		}

		internal MPElementTypeReportResource(string baseDirectory, string extractBaseDirectory)
			: base(baseDirectory, extractBaseDirectory)
		{
			m_name = "";
			m_data = "";
			m_datanode = null;
		}
		
		protected override void ProcessSingleNodeBeforeChildren(XmlNode node, ParseAction action)
		{
			if (node.Name == "ReportResource" && node.ParentNode.Name == "ReportResources")
			{
				m_name = node.Attributes["ID"].Value;
				m_data = "";
				m_datanode = null;
			}
			else if (node.Name == "ResourceData" && node.ParentNode.Name == "ReportResource")
			{
				m_data = node.InnerText;
				m_datanode = node;
			}
		}

		protected override void ProcessSingleNodeAfterChild(XmlNode node, ParseAction action)
		{
			//
		}

		protected override void ProcessSingleNodeAfterChildren(XmlNode node, ParseAction action)
		{
			if (node.Name == "ReportResource" && node.ParentNode.Name == "ReportResources")
			{
				if (action == ParseAction.ExtractNoPlaceMarks || action == ParseAction.ExtractReplaceWithPlaceMarks)
				{
					Console.WriteLine(String.Format("Report Resource found: {0}", m_name));
					SaveBinary(m_data, m_name);
				}
				else if (action == ParseAction.Embed)
				{
					Console.WriteLine(String.Format("Embeding {0}", Path.Combine(ExtractBaseDirectory, m_name)));
					string data = LoadBinary(Path.Combine(ExtractBaseDirectory, m_name));
					m_datanode.InnerText = data;
				}
			}
		}
	}
}
